<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Jedrzej\Pimpable\PimpableTrait;

class StudentResult extends Model
{
    use HasFactory, PimpableTrait;

    protected $guarded = [];

    protected $casts = [
        'content' => 'array',
        'created_at' => 'date:Y-m-d H:i:s',
        'updated_at' => 'date:Y-m-d H:i:s'
    ];
    //可搜索的字段
    protected $searchable = ['type', 'username', 'name', 'subject_id', 'all_num'];

    //可排序的字段
    protected $sortable = ['id,desc'];

    // 与考试安排的关联
    public function examination(): BelongsTo
    {
        return $this->belongsTo(Examination::class, 'examination_id', 'id');
    }

    // 与试卷的关联
    public function testList(): BelongsTo
    {
        return $this->belongsTo(TestList::class, 'test_list_id', 'id');
    }

    // 与用户的关联
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class, 'user_id', 'id')
            ->select('id','name','username');
    }

    // 与分类的关联
    public function subject()
    {
        return $this->belongsTo(Category::class, 'subject_id', 'id');
    }
}
